<template>
  <div class="boxContent">
    <div class="appBox">
      <div class="searchList">
        <el-form :model="state.params"
                 ref="FormInstance "
                 class="boxFrom">
          <div class="searchBox">
            <template v-for="(item, index) in state.totalInputColumns"
                      :key="index">

              <el-form-item v-if="item.type == 'input'">
                <el-input v-model="state.params[item.key]"
                          clearable
                          :placeholder="item.label"></el-input>
              </el-form-item>
              <el-form-item v-if="item.type == 'select'">
                <el-select v-model="state.params[item.key]"
                           :placeholder="item.label"
                           clearable
                           v-if="item.key == 'ruleStatus'">
                  <el-option label="未发布"
                             value="0" />
                  <el-option label="已发布"
                             value="1" />
                </el-select>

              </el-form-item>
              <el-form-item v-if="item.type == 'time'">
                <el-date-picker v-model="state.params[item.key]"
                                type="date"
                                value-format="YYYY-MM-DD"
                                placeholder="请选择时间" />
              </el-form-item>
            </template>
          </div>

          <el-form-item class="btns">
            <el-button v-preventReClick
                       type="primary"
                       plain
                       @click="methods.handleAction('query')">搜索</el-button>
            <el-button v-preventReClick
                       @click="methods.handleAction('reset')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="tabaleBox">
        <el-table border
                  :data="state.tableData"
                  style="width: 100%"
                  :header-cell-style="{ color: '#000' }">
          <el-table-column prop="companyName"
                           label="公司名称"
                           show-overflow-tooltip
                           align="left">
          </el-table-column>
          <el-table-column prop="userName"
                           label="操作人"
                           show-overflow-tooltip
                           align="left"
                           width="180">
          </el-table-column>
          <el-table-column prop="createdAt"
                           label="添加时间"
                           align="center"
                           width="180">
          </el-table-column>

          <el-table-column prop=""
                           label="操作"
                           align="center"
                           width="120">
            <template #default="scope">
              <div class="btnsDatil ">
                <!-- 其它操作 -->
                <!-- <div @click="methods.handleAction('edit', scope.row)"
                     v-permission="1114">编辑</div> -->
                <div @click="methods.handleAction('delete', scope.row)"
                     v-permission="1197">删除</div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination">
        <el-pagination background
                       v-model:current-page="state.params.pageNum"
                       v-model:page-size="state.params.pageSize"
                       :page-sizes="[10, 20, 30, 50]"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="state.totalSize"
                       @size-change="methods.handleSizeChange"
                       @current-change="methods.handleCurrentChange" />
      </div>

      <el-dialog v-model="state.dialogVisible"
                 v-if="state.dialogVisible"
                 title="新增推荐规则"
                 width="60%"
                 center>
        <div>
          <el-input v-model="state.valueInput"
                    size="large"
                    placeholder="请填写文章标题">
            <template #prepend> 文章标题</template>
          </el-input>

          <div class="mt20 Editor">
            <Editor ref="EditorBox"
                    v-if="state.dialogVisible"></Editor>
          </div>

          <div class="tar mt20">
            <el-button v-preventReClick
                       @click="state.dialogVisible = false"> 取消</el-button>
            <el-button v-preventReClick
                       @click="methods.handleAction('subAdd')"
                       type="primary"> 保存</el-button>
          </div>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script setup>
import {
  ref,
  onMounted,
  reactive,
  toRefs,
  defineComponent,
  getCurrentInstance,
  nextTick,
} from "vue";
import Editor from '@/components/Editor.vue'
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
const router = useRouter();
const app = getCurrentInstance()?.proxy;
const initState = () => {
  return {
    dialogVisible: false,
    totalSize: 0,
    tableData: [],
    params: {
      pageNum: 1,
      pageSize: 10,
    },
    totalInputColumns: [
      { key: "companyName", label: "公司名称", type: "input" },
      // { key: "ruleCreateName", label: "创建人", type: "input" },
      // { key: "ruleStatus", label: "选择状态", type: "select" },
    ],
    valueInput: '',
    dialogData: '',
  };
};
const state = reactive(initState());
onMounted(() => {
  methods.getDataList();
});
const methods = {

  handleAction (type, data) {
    switch (type) {
      case "edit":
        app.$router.push('/operate/recommendation/add?id=' + data.id)
        break;
      case "add": //新增
        app.$router.push('/operate/recommendation/add')
        break;
      case "subAdd": //保存
        methods.subData()
        break;
      case "delete": // 删除
        ElMessageBox.confirm("请确认是否删除该白名单企业?", "删除白名单企业", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            app.$get("/companyWhite/deleteById?id=" + data.id).then((res) => {
              if (res.code == 200) {
                methods.getDataList();
                ElMessage({
                  type: "success",
                  message: "已删除",
                });
              }
            });
          })
        break;

      case "query": //查询
        methods.getDataList();
        break;
      case "reset": // 重置
        state.params = initState().params;
        break;
    }
  },
  blurInput (data) {
    app.$post('/sysHelp/update', {
      id: data.id,
      orderNum: data.orderNum - 0
    }).then(res => {
      if (res.code == 200) {
        this.getDataList()
        state.dialogVisible = false
        ElMessage({
          message: '操作成功',
          type: 'success'
        })
      }
    })
  },
  changeSwitch (data) {
    app.$post('/recommendrule/update', {
      id: data.id,
      ruleStatus: data.ruleStatus == true ? 1 : 0,
    }).then(res => {
      if (res.code == 200) {
        this.getDataList()
        state.dialogVisible = false
        ElMessage({
          message: '操作成功',
          type: 'success'
        })
      }
    })

  },
  subData () {
    console.log();
    if (state.type == 'edit') {
      app.$post('/sysHelp/update', {
        id: state.dialogData.id,
        title: state.valueInput,
        status: state.dialogData.ruleStatus == true ? '1' : '0',
        content: app.$refs.EditorBox.valueHtml
      }).then(res => {
        if (res.code == 200) {
          this.getDataList()
          state.dialogVisible = false
          ElMessage({
            message: '操作成功',
            type: 'success'
          })
        }
      })
    }
    if (state.type == 'subAdd') {
      app.$post('/sysHelp/add', {
        title: state.valueInput,
        content: app.$refs.EditorBox.valueHtml
      }).then(res => {
        if (res.code == 200) {
          this.getDataList()
          state.dialogVisible = false
          ElMessage({
            message: '保存成功',
            type: 'success'
          })
        }
      })
    }

  },
  getDataList () {
    app.$post("/companyWhite/pageList", state.params)
      .then((res) => {
        if (res.code == 200) {
          state.tableData = res.data.records;
          state.totalSize = res.data.total;
        }
      });
  },
  //分页变化
  handleSizeChange () {
    state.params.pageNum = 1;
    methods.getDataList();
  },
  //表格翻页
  handleCurrentChange () {
    methods.getDataList();
  },
};
</script>

<style lang="scss" scoped>
.appBox {
  background: #fff;
  border-radius: 5px;
  padding: 20px;
  height: calc(100% - 20px);
  width: 100%;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;

  .tabaleBox {
    overflow: hidden;
    flex: 1;
  }
  .btnsDatil {
    color: #0079fe;
    cursor: pointer;
  }
  .pagination {
    margin-top: 20px;
    height: 50px;
    display: flex;
    justify-content: flex-end;
  }
  ::v-deep(.el-dialog__body) {
    padding: 20px 30px !important;
  }
  .imgs {
    flex-wrap: wrap;
    .forbox {
      // width: 150px;
      height: 150px;
      margin: 10px 0 0 10px;
    }
    .imgBox {
      height: 100%;
      width: auto;
    }
    .videoBox {
      width: auto;
      height: 100%;
    }
  }

  // .imgs .forbox:nth-child(4n + 1) {
  //   margin-left: 0;
  // }
}
</style>
